package com.juanLL.CMC.models;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Rect;

import com.juanLL.CMC.AssetLoader;

/**
 * Citizen class Inherits from Model and contains the logic of the citizen
 * characters in game
 * 
 */
public class Citizen extends Model {

	// boolean state variables
	public boolean isSpawned = false;
	public boolean isInitial = false;
	// random number generator
	Random rand = new Random();
	// state id constants
	public static final int STATE_INITIAL = 0;
	public static final int STATE_SPAWNED = 1;
	// speed limit control constants
	public static final int MAX_MOVE_SPEED = 7;
	public static final int MIN_MOVE_SPEED = 3;
	Rect screenRes;

	// enum that pairs a type of citizen to its bitmap representation.
	public static enum citizenEnum {
		CIT1(Model.TYPE_CITIZEN, AssetLoader.PATH_CITIZEN_MAN1);

		public int type;
		public String path;

		citizenEnum(int type, String path) {
			this.type = type;
			this.path = path;
		}
	}

	/**
	 * 
	 * @param type
	 *            the type id of this specific citizen
	 * @param bitmap
	 *            the bitmap file that represents the citizen graphically
	 */
	public Citizen(int type, Bitmap bitmap, Rect screenRes) {
		this.bitmap = bitmap;
		this.type = type;
		this.screenRes = screenRes;
		// set the default initial state at construction
		this.setState(STATE_INITIAL);
	}

	/**
	 * spawn method, spawns a citizen in the screen and sets it moving
	 * 
	 * @param screenRes
	 *            a Rect object containing screen boundaries
	 */
	public void spawn() {
		setState(STATE_SPAWNED);
		draw = true;
		x = screenRes.left - bitmap.getWidth();
		y = screenRes.bottom - bitmap.getHeight();
		xSpeed = rand.nextFloat() * (MAX_MOVE_SPEED - MIN_MOVE_SPEED)
				+ MIN_MOVE_SPEED;
		ySpeed = 0f;
	}

	/**
	 * launch method, initiates the launching of a trash object by calling the
	 * corresponding method and passing the launch origin coordinates
	 * 
	 * @param trash
	 *            the trash object to be launched
	 */
	public void launchTrash(Trash trash) {
		trash.launch(x, y);
	}

	/**
	 * setState method, changes the state of the object and updates boolean
	 * variables to reflect it
	 * 
	 * @param state
	 *            the new state to be set
	 */
	public void setState(int state) {
		switch (state) {
		case STATE_SPAWNED:
			this.state = STATE_SPAWNED;
			isSpawned = true;
			isInitial = false;
			break;
		case STATE_INITIAL:
			isInitial = true;
			isSpawned = false;
			break;
		}
	}

	@Override
	public void update() {
		if (isSpawned) {
			x += xSpeed;
			if (x >= screenRes.right)
				setState(STATE_INITIAL);
		}
	}

}
